﻿using MarkdownWeb.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MarkdownWeb.Controllers
{
    public class ContentController : Controller
    {
        // GET: Content
        public ActionResult Show(string title)
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<WikiContentContext>());
            var context = new WikiContentContext();

            var wiki = context.Wikis.FirstOrDefault(w => w.Title == title);
            if (wiki==null)
            {
                return new RedirectResult($"/Create/Index?&title={title}");
            }
            ViewData["content"] = wiki;
            return View();
        }
        [HttpPost]
        public JsonResult Delete(FormCollection form)
        {
            var title = form["ID"];
            JsonResult result = new JsonResult();
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<WikiContentContext>());
            var context = new WikiContentContext();

            var wiki = context.Wikis.FirstOrDefault(w => w.Title == title);
            if (wiki == null)
            {
                result.Data = "fail";
                return result;
            }
            context.Wikis.Remove(wiki);
            context.SaveChanges();
            result.Data = "ok";
            return result;
        }
       
    }
}